// pages/register/register.js
const app = getApp(), core = require('../../utils/core'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: 0,
    show:false,
    check:'-1',
    group_id: "",
    wx_account: null,
    mobile: "",
  },
  show:function(e) {
    this.setData({
      show:!this.data.show
    });
  },
  check:function(e){
    this.setData({
      check:e.currentTarget.dataset.index,
      group_id: e.currentTarget.dataset.index+1
    })
  },
  tab: function (e) {
    this.setData({
      nav: e.currentTarget.dataset.index
    })
  },
  inputMobile: function (event) {
    this.setData({mobile: event.detail.value});
  },
  inputPassword: function (event) {
    this.setData({password: event.detail.password});
  },
  sendCaptcha: function() {
    let page = this;
    if(page.data.countdown > 0) {
      return;
    }
    let mobile = this.data.mobile;
    if(mobile.length!==11) {
      wx.showToast({title: '请输入正确的手机号', icon:'none'});
      return;
    }
    core.post({url: '/xilujob.sms/send', data: {mobile:mobile}, success(ret, response) {
      page.setData({countdown: 60});
      let timer = setInterval(function() {
        let tick = page.data.countdown - 1;
        page.setData({countdown: tick});
        if(tick<=0) {
          clearInterval(timer);
        }
      },1000);
    }});
  },
  mobileRegister: function (event) {
    let page = this;
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        //group_id: {required:true},
        mobile: {required:true, mobile:true},
        captcha: {required:true, length:4},
        password: {required:true, minlength:6},
      },
      messages: {
        //group_id: {required:'请选择注册身份'},
        mobile: {required:'请输入手机号',mobile:'请输入正确的手机号'},
        captcha: {required: '请输入验证码', length: '请输入正确的验证码'},
        password: {required: '请输入密码',minlength:'密码长度至少6位数' },
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    data.group_id = 2;
    core.post({url:'/xilujob.user/register',data:data,success(ret, response) {
      page._registerSuccess(ret.data.userinfo);
    }});
  },
  emailRegister: function (event) {
    let page = this;
    let data = event.detail.value;
    let validator = new WeValidator({
      rules: {
        group_id: {required:true},
        username: {required:true, rangelength:[6,20]},
        email: {required:true, email:true},
        password: {required:true, minlength:[6,20]}
      },
      messages: {
        group_id: {required:'请选择注册身份'},
        username: {required:'请输入用户名',rangelength:'用户名为6到20个英文字母、数字、横线、下划线'},
        email: {required: '请输入邮箱', email: '请输入正确的邮箱'},
        password: {required: '请输入密码', rangelength: '密码最少6位，最多20位'}
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    let wxAccount = app.getWxAccount();
    if(!wxAccount||!wxAccount.openid) {
      wx.showToast({title: '请稍候，正在获取微信账号授权',icon:'none'});
      return;
    }
    data.openid = wxAccount.openid;
    data.city_id = app.getCurrentCity().id;
    core.post({url:'/user/register',data:data,success(ret, response) {
      page._registerSuccess(ret.data.userinfo);
    }});
  },
  _registerSuccess(userinfo) {
    app.setUserinfo(userinfo);
    if(userinfo.group_id==1) {
      wx.navigateBack({delta:2});
    }else {
      wx.reLaunch({url:'/pages/enterprise_audit/enterprise_audit'});
    }
    app.eventBus.emit(app.Events.UserLogin, userinfo);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({wx_account: app.getWxAccount()});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  }
})
